import inline as inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use("fivethirtyeight")
sns.set_style({'font.sans-serif': ['simhei', 'Arial']})
# matplotlib inline
loan = 2000000     # 贷款金额
annualRate = 0.06  # 贷款年利率
monthRate = annualRate/12  # 贷款月利率
period = 30  # 贷款期限30年
# 每月应还本金
monthPrincipalPayment = [loan/(period*12)]*period*12
# 每月应还利息
monthInterestPayment = [(loan - loan*n/(period*12))*monthRate+loan/(period*12) for n in range(1, period*12+1)]
# 还款期数
month = [n for n in range(1, period*12+1)]
f, ax = plt.subplots(figsize=(20, 8))
l1 = plt.bar(month, monthInterestPayment, color='b', width=0.4)
l2 = plt.bar(month, monthPrincipalPayment, color='r', width=0.4)
plt.xlabel(u'还款期数（月）', size=20)
# 设置y轴标签
plt.ylabel('还款金额（元）', size=20)
# 设置标题
plt.title(u'等额本金', size=20)
# 刻度字体大小20
plt.tick_params(labelsize=20)
# 设置注解狂
plt.legend(handles=[l1, l2, ], labels=['利息', '本金'], loc='best', fontsize=20)
plt.show()